import { request } from "../request";

// 返回数据接口
export interface GetExamDetailsRes {
  code: number;
  message: string;
  data: {
    examId: number;
    teacherId: number;
    title: string;
    description: string;
    createdAt: string;
    status: string;
    startTime: string;
    endTime: string;
    duration: number;
    classId: number;
  };
  timestamp: Record<string, unknown>;
}

// 返回数据接口
export interface GetExamFullDetailsRes {
  code: number;
  message: string;
  data: Record<string, unknown>;
  timestamp: Record<string, unknown>;
}

// 返回数据接口
export interface GetExamQuestionsRes {
  code: number;
  message: string;
  data: {
    questionId: number;
    creatorId: number;
    content: string;
    options: Record<string, unknown>;
    answer: string;
    difficulty: string;
    createdAt: Record<string, unknown>;
    type: number;
  }[];
  timestamp: Record<string, unknown>;
}

// 返回数据接口
export interface GetExamResourcesRes {
  code: number;
  message: string;
  data: {
    resourceId: number;
    chatId: number;
    userId: number;
    ossUrl: string;
    type: string;
    createdAt: Record<string, unknown>;
  }[];
  timestamp: Record<string, unknown>;
}

// 返回数据接口
export interface GetAllAvailableExamsRes {
  code: number;
  message: string;
  data: {
    examId: number;
    teacherId: number;
    title: string;
    description: string;
    createdAt: string; // 改为字符串
    status: string;
    startTime: string; // 修复：改为字符串
    endTime: string; // 修复：改为字符串
    duration: number;
    classId: number;
  }[];
  timestamp: Record<string, unknown>;
}

// 获取老师发布的考试列表返回数据接口
export interface GetPublishedExamsByTeacherRes {
  code: number;
  message: string;
  data: {
    examId: number;
    teacherId: number;
    title: string;
    description: string;
    createdAt: Record<string, unknown>;
    status: string;
  }[];
  timestamp: Record<string, unknown>;
}

// 开始考试返回数据接口
export interface StartExamRes {
  code: number;
  message: string;
  data: Record<string, unknown>;
  timestamp: Record<string, unknown>;
}

const token = localStorage.getItem("token");

// 获取考试详情
const getExamDetailsAPI = async (examId: number): Promise<GetExamDetailsRes> => {
  const token = localStorage.getItem("token");
  return request(`/api/student/exams/${examId}`, {
    method: "get",
    headers: {
      "Authorization": `Bearer ${token}`,
    },
  });
};

// 获取考试完整信息
const getExamFullDetailsAPI = async (examId: number): Promise<GetExamFullDetailsRes> => {
  const token = localStorage.getItem("token");
  return request(`/api/student/exams/${examId}/details`, {
    method: "get",
    headers: {
      "Authorization": `Bearer ${token}`,
    },
  });
};

// 获取考试题目列表
const getExamQuestionsAPI = async (examId: number): Promise<GetExamQuestionsRes> => {
  const token = localStorage.getItem("token");
  return request(`/api/student/exams/${examId}/questions`, {
    method: "get",
    headers: {
      "Authorization": `Bearer ${token}`,
    },
  });
};

// 获取考试资源列表
const getExamResourcesAPI = async (examId: number): Promise<GetExamResourcesRes> => {
  const token = localStorage.getItem("token");
  return request(`/api/student/exams/${examId}/resources`, {
    method: "get",
    headers: {
      "Authorization": `Bearer ${token}`,
    },
  });
};

/**
 * 获取所有可用的考试
 * @param {number} studentId
 * @returns
 */
const getAllAvailableExamsAPI = async (studentId: number): Promise<GetAllAvailableExamsRes> => {
  const token = localStorage.getItem("token");
  return request(`/api/student/exams/available?studentId=${studentId}`, {
    method: "get",
    headers: {
      "Authorization": `Bearer ${token}`,
    },
  });
};

// 获取老师发布的考试列表
const getPublishedExamsByTeacherAPI = async (teacherId: number): Promise<GetPublishedExamsByTeacherRes> => {
  const token = localStorage.getItem("token");
  return request(`/api/student/exams/teacher/${teacherId}`, {
    method: "get",
    headers: {
      "Authorization": `Bearer ${token}`,
    },
  });
};

// 开始考试API
const startExamAPI = async (examId: number, studentId: number): Promise<StartExamRes> => {
  const token = localStorage.getItem("token");
  return request(`/api/student/exams/${examId}/start?studentId=${studentId}`, {
    method: "post",
    headers: {
      "Content-Type": "application/x-www-form-urlencoded",
      "Authorization": `Bearer ${token}`,
    },
  });
};

export {
  getExamDetailsAPI,
  getExamFullDetailsAPI,
  getExamQuestionsAPI,
  getExamResourcesAPI,
  getAllAvailableExamsAPI,
  getPublishedExamsByTeacherAPI,
  startExamAPI
};





